# as_completed 方法：等待线程被完成，而且谁先完成则先返回谁，生成对象，可用 for 进行遍历

import time
from concurrent.futures import ThreadPoolExecutor, as_completed


def eat(name):
    print(f'{name} is eating')
    time.sleep(10 - name)
    print(f'{name} is done')
    return f"{name} 's result"


if __name__ == '__main__':
    with ThreadPoolExecutor(max_workers=5) as executor:
        req = as_completed([executor.submit(eat, name) for name in range(10)])
        print(list(i.result() for i in req))  # 先完成先返回
